module.exports = function() {
	this.robotData = require("../../../../conf/robot.json")
	this.matchList = {}         //匹配中玩家列表
	this.expectCount = 3   		//匹配需要人数
	this.maxRobotNum = this.robotData.length
	this.curRobotNum = Math.floor(Math.random() * this.maxRobotNum)
	//加入匹配
	this.joinMatching = function(player) {
		this.matchList[player.uid] = player
		return true
	}
	//离开匹配
	this.leaveMatching = function(uid) {
		delete this.matchList[uid]
	}
	//匹配前
	this.beforeRunMatch= function() {
		this.runMatch()
	}
	//运行匹配
	this.runMatch = function() {
		// console.log("runMatch : "+this.matchType)
		var tmpList = []
		for(var i in this.matchList){
			tmpList.push(this.matchList[i])
		}
		if(tmpList.length < this.expectCount){
			if(tmpList.length > 0){
				//有概率加入机器人
				var rand = Math.random()
				console.log(rand)
				if(rand > 0.4){
					this.joinRobot()
				}
			}
			return
		}
		//乱序
	    var len = tmpList.length;
	    for (var i = 0; i < len - 1; i++) {
	        var index = parseInt(Math.random() * (len - i));
	        var temp = tmpList[index];
	        tmpList[index] = tmpList[len - i - 1];
	        tmpList[len - i - 1] = temp;
	    }
		while(tmpList.length >= this.expectCount){
			var tmpPlayers = tmpList.splice(0,this.expectCount)
			for(var i = 0;i <  tmpPlayers.length;i++){
				delete this.matchList[tmpPlayers[i].uid]
			}
			this.matchSuccess(tmpPlayers)
		}
		// console.log("剩余玩家")
		// console.log( this.matchList)
	}
	this.joinRobot = function() {
		var uid = Math.floor(this.curRobotNum + Math.random() * 10 + 1) % this.maxRobotNum
		if(!uid){
			uid = Math.floor(Math.random() * 10 + 1)
		}
		this.curRobotNum = uid
		var player = {
			"uid" : uid,
			"nickname" : this.robotData[uid].nickname,
			"head" : this.robotData[uid].head,
			"allGame" : Math.floor(Math.random() * 100) + 200,
			"allWin" :  Math.floor(Math.random() * 50) + 70,
			"maxWin" : Math.floor(Math.random() * 6) + 6,
			"curWin" : Math.floor(Math.random() * 6)
		}
		if(this.data && this.data.rate){
			player.maxScore = Math.pow(2,Math.floor(Math.random() * 3) + 1)  * 100 * this.data.rate
		}else{
			player.maxScore = Math.pow(2,Math.floor(Math.random() * 7) + 1)  * 1000
		}
		this.joinMatching(player)
	}
	//匹配成功
	this.matchSuccess = function() {
		console.log(new Error("matchSuccess是抽象方法"))
	}
	//创建匹配房间成功
	this.matchRoomBegin = function(roomId,players){
	}
	//匹配房间结束
	this.matchRoomOver = function(roomId,info) {
	}
	//玩家退出
	this.userQuit = function(roomId,uid) {
		// body...
	}
}